package gemini.pollux.ui.client.page.content.product.producttype.list;

import gemini.basic.model.ProductType;
import gemini.pollux.ui.client.ServiceCatalog;
import gemini.pollux.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetDisplay;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetPresenter;
import gemini.pollux.ui.client.page.RootEvent;
import gemini.pollux.ui.client.page.content.product.producttype.ProductTypeEvent;
import gemini.pollux.ui.client.page.content.product.producttype.widget.ProductTypeTableEvent;
import gemini.pollux.ui.client.page.content.product.producttype.widget.ProductTypeTableHandler;
import gemini.pollux.ui.client.page.content.product.producttype.widget.ProductTypeTableWidget;
import gemini.pollux.ui.client.utils.GuiUtils;
import gemini.pollux.ui.client.utils.UIConstants;

import java.util.ArrayList;
import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.inject.Inject;

public class ProductTypeListPresenter extends
		PolluxWidgetPresenter<ProductTypeListPresenter.Display> {

	@Inject
	public ProductTypeListPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends PolluxWidgetDisplay {
		void showProductTypes(List<ProductType> productTypes);

		ProductTypeTableWidget getProductTypeTableWidget();
	}

	public static final Place PLACE = new Place(UIConstants.PLACE_PRODUCT_TYPE);
	private ProductType outputObject;
	
	
	@Override
	protected void onBind() {
		registerHandler(display.getProductTypeTableWidget().addProductTypeActionHandler(new ProductTypeTableHandler() {
			
			@Override
			public void onEdit(ProductTypeTableEvent event) {
				outputObject = event.getProductType();
				localEventBus.fireEvent(new ProductTypeEvent(ProductTypeEvent.ForwardType.EDIT));
			}
			
			@Override
			public void onDelete(ProductTypeTableEvent event) {
				eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
				ServiceCatalog.getProductTypeService().deleteProductType(event.getProductType().getId(), 
						new AbstractAsyncCallback<Void>() {

							@Override
							public void onSuccess(Void result) {
								localEventBus.fireEvent(new ProductTypeEvent(ProductTypeEvent.ForwardType.BACK_TO_LIST));
							}
							
							@Override
							public void onFailure(final Throwable caught) {
								eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.UN_MASK));
								GuiUtils.alert("This Product Type is in used! Cannot delete!");
							}
						});
			}
			
			@Override
			public void onAdd(ProductTypeTableEvent event) {
				localEventBus.fireEvent(new ProductTypeEvent(ProductTypeEvent.ForwardType.NEW));
			}
		}));
		
		loadProductTypes();
	}
	
	private void loadProductTypes() {
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getProductTypeService().getAllProductTypes(
				new AbstractAsyncCallback<ArrayList<ProductType>>() {

					@Override
					public void onSuccess(ArrayList<ProductType> result) {
						eventBus.fireEvent(
								new RootEvent(RootEvent.ForwardType.UN_MASK));
						display.initCustom();
						display.showProductTypes(result);
					}
				});
	}

	@Override
	public void setInputObject(Object ob){
	}


	@Override
	public Place getPlace() {
		return PLACE;
	}
	
	@Override
	public Object getOutputObject(){
		return this.outputObject;
	}
}
